#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <arpa/inet.h>
#include "logger.h"

//socket()初始化、connect()连接
int connect_server(int port, char *servip)
{
	int			rv = -1;
	int			clifd = -1;
	struct	sockaddr_in	servaddr;

	clifd = socket(AF_INET, SOCK_STREAM, 0);
	if(clifd < 0)
	{
		log_error("Socket() create clifd failure: %s\n", strerror(errno));
		return -1;
	}

	memset(&servaddr, 0, sizeof(servaddr));                
	servaddr.sin_family = AF_INET;                
	servaddr.sin_port = htons(port); 
	inet_aton(servip, &(servaddr.sin_addr));
	rv = connect(clifd, (struct sockaddr *)&servaddr, sizeof(servaddr));
	if(rv < 0)
	{
		log_error("Client[%d] get connected with server failure: %s\n", clifd, strerror(errno));
		close(clifd);
		return -2;
	}

	return clifd;

}

	
